package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.framework.core.result.APIObjResponse;
import com.framework.core.result.BasePage;
import com.project.base.BaseController;
import com.project.biz.CmsUserAddressBiz;
import com.project.common.model.UserInfo;
import com.project.common.utils.IpageToPage;
import com.project.entity.CmsUserAddress;
import com.project.pojo.dto.PageReqModel;
import com.project.pojo.model.UserAddressAddOrEditModel;
import com.project.pojo.dto.UserAddressDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description: 地址管理
 * @Author: zhuchi
 * @CreateDate: 2020/10/15 1:48
 * @Version: 1.0
 */
@Api(value = "CmsUserAddressController", description = "地址管理")
@RestController
@RequestMapping("/userAddress")
@Slf4j
public class CmsUserAddressController extends BaseController {

    /**
     * 地址管理业务层接口类
     */
    @Autowired
    private CmsUserAddressBiz userAddressBiz;

    /**
     * 获取用户收货地址
     * @param pageReqModel
     * @return
     */
    @ApiOperation(value = "获取用户收货地址", httpMethod = "POST")
    @PostMapping(value = "/getAddress")
    public APIObjResponse<BasePage<UserAddressDTO>> getAddress(@RequestBody PageReqModel pageReqModel){
        UserInfo userInfo = super.getLoginUser();
        IPage<CmsUserAddress> iPage = this.userAddressBiz.getAddress(pageReqModel.getPageNo(), pageReqModel.getPageSize(), userInfo.getBaseUserId());
        BasePage<UserAddressDTO> basePage = IpageToPage.toBasePage(iPage, UserAddressDTO.class);
        return APIObjResponse.success("查询成功", basePage);
    }

    /**
     * 获取用户收货地址详情
     * @param addressId 收货地址ID
     * @return
     */
    @ApiOperation(value = "获取用户收货地址详情", httpMethod = "GET")
    @GetMapping(value = "/getAddressDetail")
    public APIObjResponse<UserAddressDTO> getAddressDetail(@RequestParam("addressId") Long addressId){
        UserInfo userInfo = super.getLoginUser();
        if (null == addressId){
            return APIObjResponse.error("收货地址ID不能为空!");
        }
        CmsUserAddress userAddress = this.userAddressBiz.getById(addressId);
        if (null == userAddress || userAddress.getStatus().equals(-1)){
            return APIObjResponse.error("收货地址信息不存在或已删除!");
        }
        if (!userAddress.getBaseUserId().equals(userInfo.getBaseUserId())){
            return APIObjResponse.error("该收货地址不属于当前用户!");
        }
        UserAddressDTO userAddressDTO = new UserAddressDTO();
        BeanUtils.copyProperties(userAddress, userAddressDTO);
        return APIObjResponse.success("查询成功", userAddressDTO);
    }

    /**
     * 获取用户默认收货地址
     * @return
     */
    @ApiOperation(value = "获取用户默认收货地址", httpMethod = "GET")
    @GetMapping(value = "/getDefaultAddress")
    public APIObjResponse<UserAddressDTO> getDefaultAddress(){
        UserInfo userInfo = super.getLoginUser();
        CmsUserAddress userAddress = this.userAddressBiz.getDefaultAddress(userInfo.getBaseUserId());
        UserAddressDTO userAddressDTO = null;
        if (null != userAddress){
            userAddressDTO = new UserAddressDTO();
            BeanUtils.copyProperties(userAddress, userAddressDTO);
        }
        return APIObjResponse.success("查询成功", userAddressDTO);
    }

    /**
     * 新增/修改收货地址
     * @param userAddressAddOrEditModel
     * @return
     */
    @ApiOperation(value = "新增/修改收货地址", httpMethod = "POST")
    @PostMapping(value = "/addOrEditAddress")
    public APIObjResponse<Long> addOrEditAddress(@Validated @RequestBody UserAddressAddOrEditModel userAddressAddOrEditModel){
        UserInfo userInfo = super.getLoginUser();
        log.info("新增/修改收货地址入参：" + userAddressAddOrEditModel);
        userAddressAddOrEditModel.setBaseUserId(userInfo.getBaseUserId());
        Long result = this.userAddressBiz.addOrEditAddress(userAddressAddOrEditModel);
        return APIObjResponse.success((userAddressAddOrEditModel.getAddressId() == null ? "新增" : "修改") + "成功", result);
    }

    /**
     * 删除收货地址
     * @param addressId
     * @return
     */
    @ApiOperation(value = "删除收货地址", httpMethod = "GET")
    @GetMapping(value = "/deleteAddress")
    public APIObjResponse<Boolean> deleteAddress(@RequestParam("addressId") Long addressId){
        UserInfo userInfo = super.getLoginUser();
        if (null == addressId){
            return APIObjResponse.error("收货地址ID不能为空!");
        }
        CmsUserAddress userAddress = this.userAddressBiz.getById(addressId);
        if (!userAddress.getBaseUserId().equals(userInfo.getBaseUserId())){
            return APIObjResponse.error("该收货地址不属于当前用户!");
        }
        userAddress.setUpdateUser(userInfo.getBaseUserId());
        userAddress.setUpdateTime(new Date());
        userAddress.setStatus(-1);
        boolean result = this.userAddressBiz.updateById(userAddress);

        return APIObjResponse.success("操作成功", result);
    }

}
